package com.bwsoft.api.action;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.QualityControlJoiner;
import com.bwsoft.admin.domain.valueObject.QualityControlJoinerVO;
import com.bwsoft.admin.service.QualityControlService;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.util.ValidatorUtil;

public class QualityControlAction extends ApiBaseAction<QualityControlAction> {
	private static Logger LOG = Logger.getLogger(QualityControlAction.class);

	private String userid;
	private String field;
	private String scope;
	private String count;
	private String nature;
	private String standard;
	private String search;
	
	private String sponsor;
	private String projectName;
	private Date beginDate;
	private Date feedBackDate;
	private Date endDate;
	private String description;
	private String other;
	private String ptclsj;
	private String gbjgxx;
	private String ptxz;
	private String id;
	private String price;
	
	private String joiner;
	
	private Boolean canyuFlag;
	
	private List<QualityControlJoinerVO> controlJoinerVOs;
	@Resource(name = QualityControlService.NAME)
	private QualityControlService adminControlService;
	
	/**
	 * 2.3.1 获取质量控制列表
	 * qualityControl_getQualityControlList.action?userid=80001&field=1&scope=1&count=1&nature=1&search=&page=1
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void getQualityControlList() throws IOException, Exception {
		LOG.info("=================================开始调用【2.3.1 获取质量控制列表 getQualityControlList】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		LOG.info("传入参数r="+super.getR());
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();

		if(!parameMap.isEmpty()&&parameMap.containsKey("userid")) userid=(String) parameMap.get("userid");
		if(!parameMap.isEmpty()&&parameMap.containsKey("field")) field=(String) parameMap.get("field");
		if(!parameMap.isEmpty()&&parameMap.containsKey("scope")) scope=(String) parameMap.get("scope");
		if(!parameMap.isEmpty()&&parameMap.containsKey("count")) count=(String) parameMap.get("count");
		if(!parameMap.isEmpty()&&parameMap.containsKey("nature")) nature=(String) parameMap.get("nature");
		if(!parameMap.isEmpty()&&parameMap.containsKey("search")) search=(String) parameMap.get("search");
		if(!parameMap.isEmpty()&&parameMap.containsKey("page")) page=Integer.parseInt((String) parameMap.get("page"));
		
		
		//如果page为null则赋值默认值取第一页
		page=(page==null||page<=0)?1:page;
		
		if(userid==null||userid.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "用户编号不能为空！");
			canFlag=false;
		}
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = qualityControlService.getQualityControlList(userid,field,scope,count,nature,search,page);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		}catch (Exception e) {
			e.printStackTrace();
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			LOG.info("【2.3.1 获取质量控制列表 getQualityControlList】异常"+e);
		}
		finishMethod();
		LOG.info("=================================调用结束【2.3.1 获取质量控制列表 getQualityControlList】获取资讯类别==============================");
	}
	
	
	/**
	 * 2.3.2发起质量控制
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void submit() throws IOException, Exception {
		LOG.info("=================================开始调用【2.3.2发起质量控制 submit】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入参数r="+super.getR());
		LOG.info("传入参数：【sponsor="+sponsor+"】【projectName="+projectName+"】【field="+field+"】【scope="+scope+"】【count="+count+"】" +
				"【standard="+standard+"】【nature="+nature+"】【description="+description+"】【other="+other+"】【beginDate="+beginDate+"】【feedBackDate="+feedBackDate+"】" +
						"【endDate="+endDate+"】【ptclsj="+ptclsj+"】【gbjgxx="+gbjgxx+"】【ptxz="+ptxz+"】【price="+price+"】");
		
			if (sponsor == null || sponsor.trim().equals("")
					|| projectName == null || projectName.trim().equals("")
					|| field == null || field.trim().equals("")
					|| scope == null || scope.trim().equals("")
					|| count == null || count.trim().equals("")
					|| nature == null || nature.trim().equals("")
					|| standard == null || standard.trim().equals("")
							|| price == null || price.trim().equals("")
					|| description == null || description.trim().equals("")
					|| other == null || other.trim().equals("")
					|| beginDate == null || feedBackDate == null
					|| endDate == null || ptclsj == null || gbjgxx == null
					|| ptxz == null

			) {
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "参数不能为空！");
				canFlag = false;
			}
			if(canFlag&&projectName.length()>20){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "项目名称不能大于20位！");
				canFlag=false;
			}
			if(canFlag&&imgContentType!=null&& !userService.isImage(imgContentType)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "上传的文件不是图片类型！");
				canFlag=false;
			}
			if(canFlag&&img!=null&& !userService.isFitSize(img)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "上传的图片不能超过8M！");
				canFlag=false;
			}
			if(canFlag&&!ValidatorUtil.isPrice(price)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "price 格式不正确");
				canFlag=false;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			System.out.println();
			if(canFlag&&!beginDate.after(calendar.getTime())){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "开始时间不能小于当前时间");
				canFlag=false;
			}
			if(canFlag&&!endDate.after(beginDate)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "结束时间必须大于开始时间");
				canFlag=false;
			}
			if(canFlag&&!feedBackDate.after(endDate)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "反馈时间必须大于结束时间");
				canFlag=false;
			}
		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = qualityControlService.submitQualityControl(sponsor,projectName,field,scope,count,standard,nature,price,description,other,beginDate,feedBackDate,endDate,ptclsj.equals("1"),gbjgxx.equals("1"),ptxz.equals("1"),img,imgFileName);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		}catch (Exception e) {
			e.printStackTrace();
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			LOG.info("【2.3.1 获取质量控制列表 getQualityControlList】异常"+e);
		}
		finishMethod();
		LOG.info("=================================调用结束【2.3.2发起质量控制 submit】==============================");
	}
	
	
	/**
	 * 2.3.2发起质量控制
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void submitAndroid() throws IOException, Exception {
		LOG.info("=================================开始调用【2.3.2发起质量控制 submit】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入参数r="+super.getR());
		LOG.info("传入参数：【sponsor="+sponsor+"】【projectName="+projectName+"】【field="+field+"】【scope="+scope+"】【count="+count+"】" +
				"【standard="+standard+"】【nature="+nature+"】【description="+description+"】【other="+other+"】【beginDate="+beginDate+"】【feedBackDate="+feedBackDate+"】" +
						"【endDate="+endDate+"】【ptclsj="+ptclsj+"】【gbjgxx="+gbjgxx+"】【ptxz="+ptxz+"】【price="+price+"】");
		
			if (sponsor == null || sponsor.trim().equals("")
					|| projectName == null || projectName.trim().equals("")
					|| field == null || field.trim().equals("")
					|| scope == null || scope.trim().equals("")
					|| count == null || count.trim().equals("")
					|| nature == null || nature.trim().equals("")
					|| standard == null || standard.trim().equals("")
							|| price == null || price.trim().equals("")
					|| description == null || description.trim().equals("")
					|| other == null || other.trim().equals("")
					|| beginDate == null || feedBackDate == null
					|| endDate == null || ptclsj == null || gbjgxx == null
					|| ptxz == null

			) {
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "参数不能为空！");
				canFlag = false;
			}
			if(canFlag&&projectName.length()>20){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "项目名称不能大于20位！");
				canFlag=false;
			}
			/*if(canFlag&&imgContentType!=null&& !userService.isImage(imgContentType)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "上传的文件不是图片类型！");
				canFlag=false;
			}
			if(canFlag&&img!=null&& !userService.isFitSize(img)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "上传的图片不能超过8M！");
				canFlag=false;
			}*/
			if(canFlag&&!ValidatorUtil.isPrice(price)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "价格格式不正确");
				canFlag=false;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			System.out.println();
			if(canFlag&&!beginDate.after(calendar.getTime())){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "开始时间不能小于当前时间");
				canFlag=false;
			}
			if(canFlag&&!endDate.after(beginDate)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "结束时间必须大于开始时间");
				canFlag=false;
			}
			if(canFlag&&!feedBackDate.after(endDate)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "反馈时间必须大于结束时间");
				canFlag=false;
			}
		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			img = new File[3];
			img[0]=img1;
			img[1]=img2;
			img[2]=img3;
			imgFileName=new String[3];
			imgFileName[0]=img1FileName;
			imgFileName[1]=img2FileName;
			imgFileName[2]=img3FileName;
			ResponsePojo rp = qualityControlService.submitQualityControl(sponsor,projectName,field,scope,count,standard,nature,price,description,other,beginDate,feedBackDate,endDate,ptclsj.equals("1"),gbjgxx.equals("1"),ptxz.equals("1"),img,imgFileName);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		}catch (Exception e) {
			e.printStackTrace();
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			LOG.info("【2.3.1 获取质量控制列表 getQualityControlList】异常"+e);
		}
		finishMethod();
		LOG.info("=================================调用结束【2.3.2发起质量控制 submit】==============================");
	}

	
	/**
	 * 2.3.3质量控制详情
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings({ "unchecked"})
	public void info() throws Exception {

		LOG.info("=================================开始调用【2.3.3质量控制详情 info】==============================");
		
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入加密参数r="+r);
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();
		
		if(!parameMap.isEmpty()&&parameMap.containsKey("id")) id=((String) parameMap.get("id"));
		if(!parameMap.isEmpty()&&parameMap.containsKey("userid")) userid=((String) parameMap.get("userid"));
       
		if(getUserid()==null||getUserid().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "userid不能为空！");
			canFlag=false;
		}
		
		if(id==null||id.equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "id不能为空！");
			canFlag=false;
		}
		
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = qualityControlService.qualityControlInfo(userid,id);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		
		finishMethod();
		LOG.info("=================================调用结束【2.3.3质量控制详情 info】==============================");
	}
	
	/**
	 * 2.3.4参与质量控制
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings({ "unchecked"})
	public void joinIn() throws Exception {

		LOG.info("=================================开始调用【2.3.4参与质量控制 joinIn】==============================");
		
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		
		LOG.info("传入加密参数r="+r);
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();
		
		if(!parameMap.isEmpty()&&parameMap.containsKey("id")) id=((String) parameMap.get("id"));
		if(!parameMap.isEmpty()&&parameMap.containsKey("joiner")) joiner=((String) parameMap.get("joiner"));
       
		if(joiner==null||joiner.equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "joiner不能为空！");
			canFlag=false;
		}
		
		if(id==null||id.equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "id不能为空！");
			canFlag=false;
		}
		
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = qualityControlService.qualityControlJoinIn(joiner,id);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		
		finishMethod();
		LOG.info("=================================调用结束【2.3.4参与质量控制 joinIn】==============================");
	}
	
	
	/**
	 * 2.5.3我参与的质量控/我发起的质量控制
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public void myList() throws IOException, Exception {
		LOG.info("=================================开始调用【2.5.3我参与的质量控/我发起的质量控制 getQualityControlList】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		LOG.info("传入参数r="+super.getR());
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();

		if(!parameMap.isEmpty()&&parameMap.containsKey("userid")) userid=(String) parameMap.get("userid");
		if(!parameMap.isEmpty()&&parameMap.containsKey("canyuFlag")) canyuFlag=parameMap.get("canyuFlag").equals("1");
		if(!parameMap.isEmpty()&&parameMap.containsKey("page")) page=Integer.parseInt( (String) parameMap.get("page"));
		
		
		//如果page为null则赋值默认值取第一页
		page=(page==null||page<=0)?1:page;
		
		if(userid==null||userid.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "用户编号不能为空！");
			canFlag=false;
		}
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			ResponsePojo rp = qualityControlService.myList(userid,canyuFlag,page);
			LOG.info("调用取数方法结束");
			responseJson.put("errcode", rp.getErrcode());
			responseJson.put("errmsg", rp.getErrmsg());
			responseJson.put("data", rp.getData());
		}
		}catch (Exception e) {
			e.printStackTrace();
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			LOG.info("【2.3.1 获取质量控制列表 getQualityControlList】异常"+e);
		}
		finishMethod();
		LOG.info("=================================调用结束【2.3.1 获取质量控制列表 getQualityControlList】获取资讯类别==============================");
	}
	
	
	
	public String joinerPage(){
		LOG.info("=================================开始调用【参加质量控制人员页面】==============================");
		try{
		//得到调用接口开始时的毫秒数
		beginTimestamp=Calendar.getInstance().getTimeInMillis();
		Boolean canFlag=true;
		LOG.info("传入参数r="+super.getR());
		if(r==null||r.trim().equals("")){
			responseJson.put("errcode", 1);
			responseJson.put("errmsg", "r值不能为空");
			finishMethod();
			LOG.info("=======================================传入参数r为空，提前结束");
		}
		Map<String, Object> parameMap= reflexParameters();

		if(!parameMap.isEmpty()&&parameMap.containsKey("id")) id=(String) parameMap.get("id");
		
		
		if(id==null||id.trim().equals("")){
			return "joinerPageError";
		}
		

		if (canFlag) {
			// 调用service方法得到要返回的数据对象
			LOG.info("开始调用取数方法");
			List<QualityControlJoiner> controlJoiners=adminControlService.findJoinersData(1, 1000, Integer.parseInt(id));
			controlJoinerVOs=adminControlService.getJoinerVo(controlJoiners);
		}
		}catch (Exception e) {
			e.printStackTrace();
			return "joinerPageError";
		}
		LOG.info("=================================调用结束【参加质量控制人员页面】==============================");
		return "joinerPage";
	}
	

	public String getUserid() {
		return userid;
	}


	public void setUserid(String userid) {
		this.userid = userid;
	}


	public String getField() {
		return field;
	}


	public void setField(String field) {
		this.field = field;
	}


	public String getScope() {
		return scope;
	}


	public void setScope(String scope) {
		this.scope = scope;
	}


	public String getCount() {
		return count;
	}


	public void setCount(String count) {
		this.count = count;
	}


	public String getStandard() {
		return standard;
	}


	public void setStandard(String standard) {
		this.standard = standard;
	}


	public String getSearch() {
		return search;
	}


	public void setSearch(String search) {
		this.search = search;
	}


	public String getSponsor() {
		return sponsor;
	}


	public void setSponsor(String sponsor) {
		this.sponsor = sponsor;
	}


	public String getProjectName() {
		return projectName;
	}


	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}


	public Date getBeginDate() {
		return beginDate;
	}


	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}


	public Date getFeedBackDate() {
		return feedBackDate;
	}


	public void setFeedBackDate(Date feedBackDate) {
		this.feedBackDate = feedBackDate;
	}


	public Date getEndDate() {
		return endDate;
	}


	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public String getOther() {
		return other;
	}


	public void setOther(String other) {
		this.other = other;
	}


	public String getPtclsj() {
		return ptclsj;
	}


	public void setPtclsj(String ptclsj) {
		this.ptclsj = ptclsj;
	}


	public String getGbjgxx() {
		return gbjgxx;
	}


	public void setGbjgxx(String gbjgxx) {
		this.gbjgxx = gbjgxx;
	}


	public String getPtxz() {
		return ptxz;
	}


	public void setPtxz(String ptxz) {
		this.ptxz = ptxz;
	}


	public String getNature() {
		return nature;
	}


	public void setNature(String nature) {
		this.nature = nature;
	}


	public String getPrice() {
		return price;
	}


	public void setPrice(String price) {
		this.price = price;
	}


	public List<QualityControlJoinerVO> getControlJoinerVOs() {
		return controlJoinerVOs;
	}


	public void setControlJoinerVOs(List<QualityControlJoinerVO> controlJoinerVOs) {
		this.controlJoinerVOs = controlJoinerVOs;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public QualityControlService getAdminControlService() {
		return adminControlService;
	}


	public void setAdminControlService(QualityControlService adminControlService) {
		this.adminControlService = adminControlService;
	}


	
}
